const os = require('os');

function getLocalIP() {
  const interfaces = os.networkInterfaces();
  // console.log('检测到网络接口:', Object.keys(interfaces).join(', '));
  // console.log('详细接口信息:', JSON.stringify(interfaces, null, 2));

  for (const interfaceName of Object.keys(interfaces)) {
    const addresses = interfaces[interfaceName];
    
    for (const address of addresses) {
      if (
        address.family === 'IPv4' &&
        !address.internal &&
        /^(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.)/.test(address.address)
      ) {
        return address.address;
      }
    }
  }
  
  return 'localhost';
}

module.exports = { getLocalIP };